<?php
/**
 * @author GhostWei
 * @date Aug 22, 2012
 */
class system_user_controller_photo extends system_user_controller_panel {
	
	protected $concept = null;
	public function init() {
		if(!parent::init()) {
			return ;
		}
		picture_category::$_lang = $this->user->getLang();
		$this->concept = new picture_photo_localeConcept($this->user->getLang());
		$this->_crumbs()->addPage(system_nav_page::create($this->language('Pictures'))->setRoute('picture/index'));
		$this->_crumbs()->addPage(system_nav_page::create($this->language('Photos'))->setRoute('picture/photo/index'));
	}

	protected function _photoList() {
		$adapter = new system_paginator_adapter_concept(new picture_photo_concept($this->user->getLang()));
		$adapter->setCurrentPageNumber(intval($this->getRequest()->getParam('page', 1)));
		$keyword = trim($this->_get('keyword'));
		if($keyword == self::$_keyword) $keyword = '';
		if($keyword) {
			$adapter->where("locale.`title` LIKE '%$keyword%'");
		}
		$this->view->order = $order = strtolower($this->getRequest()->getQuery('order'));
		$_order = array("creationtime DESC", "display_order ASC");
		if($order) {
			$this->view->sort = $sort = intval($this->getRequest()->getQuery('sort'));
			switch($order) {
				case 'date': {
					$order = 'lastmodified';
					break;
				}
				default: {
					$sort = !$sort;
					$order = 'display_order';
					break;
				}
			}
			$_sort = !$sort ? 'DESC' : 'ASC';
			$_order = array("$order $_sort");
		}
		$adapter->order($_order);
		$adapter->setLimit(48);
		$this->view->list = new system_paginator($adapter);
	}

	protected function _saveRow($row) {
		if(!$this->getRequest()->isPost()) {
			return $row;
		}
		$this->referer = $this->_post('referer');
		$row->getLocale()->setTitle(trim($this->_post('title')));
		$row->getLocale()->setDescription($this->_post('description'));
		$row->setDisplayOrder(intval($this->_post('display_order')));
		$row->setIsActive(intval($this->_post('is_active')) ? 1 : 0);
		$row->setKeywords(trim($this->_post('tag')));
		if(!isset($_FILES['hash']) || (!is_uploaded_file($_FILES['hash']['tmp_name']) && !$row->getId())) {
			$this->view->throwException = "Please select a Photo you want to upload";
			return $row;
		}
		$this->_saveToDatabase($row, 'hash', 'image');
		return $row;
	}

}
